<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>C语言指针 | Jixer的小屋</title><meta name="author" content="Jixer"><meta name="copyright" content="Jixer"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="文章写于2022年C语言培训指针章节  _是什么指针？什么是指针变量_？  指针就是地址 指针变量就是存放地址的变量 指针也是一个数据类型  一.指针变量1.一级指针定义指针变量 12int a &#x3D; 5; &#x2F;&#x2F;空间分配4个字节int *p &#x3D; &amp;a；&#x2F;&#x2F;&amp;a表示a的地址  0000100002000030000400005  假设地址 注意：每次程序重新运行，分配的地址都会变化!">
<meta property="og:type" content="article">
<meta property="og:title" content="C语言指针">
<meta property="og:url" content="http://www.lijunxi.site/posts/3447396109/index.html">
<meta property="og:site_name" content="Jixer的小屋">
<meta property="og:description" content="文章写于2022年C语言培训指针章节  _是什么指针？什么是指针变量_？  指针就是地址 指针变量就是存放地址的变量 指针也是一个数据类型  一.指针变量1.一级指针定义指针变量 12int a &#x3D; 5; &#x2F;&#x2F;空间分配4个字节int *p &#x3D; &amp;a；&#x2F;&#x2F;&amp;a表示a的地址  0000100002000030000400005  假设地址 注意：每次程序重新运行，分配的地址都会变化!">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://q1.qlogo.cn/g?b=qq&nk=2770063826&s=640">
<meta property="article:published_time" content="2024-01-17T05:32:11.000Z">
<meta property="article:modified_time" content="2024-05-07T03:10:23.202Z">
<meta property="article:author" content="Jixer">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://q1.qlogo.cn/g?b=qq&nk=2770063826&s=640"><link rel="shortcut icon" href="/img/logo/favicon.ico"><link rel="canonical" href="http://www.lijunxi.site/posts/3447396109/index.html"><link rel="preconnect"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="/pluginsSrc/@fortawesome/fontawesome-free/css/all.min.css?v=6.5.1"><link rel="stylesheet" href="/pluginsSrc/@fancyapps/ui/dist/fancybox/fancybox.css?v=5.0.33" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: '/pluginsSrc/@egjs/infinitegrid/dist/infinitegrid.min.js?v=4.11.1',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'C语言指针',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-05-07 11:10:23'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom-all-min.css"><link rel="stylesheet" href="/css/custom-fancybox-min.css"><link rel="stylesheet" href="/css/custom-share-min.css"><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="" data-original="https://q1.qlogo.cn/g?b=qq&amp;nk=2770063826&amp;s=640" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="Jixer的小屋"><span class="site-name">Jixer的小屋</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">C语言指针</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-17T05:32:11.000Z" title="发表于 2024-01-17 13:32:11">2024-01-17</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-05-07T03:10:23.202Z" title="更新于 2024-05-07 11:10:23">2024-05-07</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="C语言指针"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>文章写于2022年C语言培训指针章节</p>
</blockquote>
<p>_是什么指针？什么是指针变量_？</p>
<ul>
<li><strong>指针就是地址</strong></li>
<li><strong>指针变量就是存放地址的变量</strong></li>
<li><strong>指针也是一个数据类型</strong></li>
</ul>
<h3 id="一-指针变量"><a href="#一-指针变量" class="headerlink" title="一.指针变量"></a>一.指针变量</h3><h4 id="1-一级指针"><a href="#1-一级指针" class="headerlink" title="1.一级指针"></a>1.一级指针</h4><p><strong>定义指针变量</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int a = 5; //空间分配4个字节</span><br><span class="line">int *p = &amp;a；//&amp;a表示a的地址</span><br></pre></td></tr></table></figure>

<table><tbody><tr><td class="has-text-align-center" data-align="center">00001</td></tr><tr><td class="has-text-align-center" data-align="center">00002</td></tr><tr><td class="has-text-align-center" data-align="center">00003</td></tr><tr><td class="has-text-align-center" data-align="center">00004</td></tr><tr><td class="has-text-align-center" data-align="center">00005</td></tr></tbody></table>

<p>假设地址</p>
<p><em>注意：每次程序重新运行，分配的地址都会变化!</em></p>
<p>p : &amp;a —–&gt; a ：5 表示p指向a，则a就为p的<strong>目标变量</strong></p>
<p><strong>访问</strong></p>
<ul>
<li>直接访问：printf(“%d”,a);</li>
<li>间接访问：printf(“%d”,*p);</li>
<li>a+5 &#x3D;&#x3D; *p+5</li>
</ul>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int a = 100,b = 5;</span><br><span class="line">int *p1 = &amp;a,*p2 = &amp;b;</span><br><span class="line">printf(&quot;%d,%d\n&quot;,a,b);</span><br><span class="line">printf(&quot;%d,%d\n&quot;,*p1,*p2);</span><br></pre></td></tr></table></figure>

<p>1.指针指向改变，但目标变量没变</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int a = 100, b = 5,*p1,*p2,*c;</span><br><span class="line">p1 = &amp;a; p2 = &amp;b;</span><br><span class="line">c = p1; p1 = p2; p2 = c;</span><br><span class="line">printf(&quot;%d %d\n&quot;,a,b);</span><br><span class="line">printf(&quot;%d %d&quot;,*p1,*p2);</span><br></pre></td></tr></table></figure>

<p>2.指针指向不变，目标变量改变</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int a = 100, b = 5,*p1,*p2,c=0;</span><br><span class="line">p1 = &amp;a; p2 = &amp;b;</span><br><span class="line">c = *p1; *p1 = *p2; *p2 = c;</span><br><span class="line">printf(&quot;%d %d\n&quot;,a,b);</span><br><span class="line">printf(&quot;%d %d&quot;,*p1,*p2);</span><br></pre></td></tr></table></figure>

<h4 id="2-二级指针（指向指针的指针变量）"><a href="#2-二级指针（指向指针的指针变量）" class="headerlink" title="2.二级指针（指向指针的指针变量）"></a>2.<strong>二级指针</strong>（指向指针的指针变量）</h4><p>二级指针：表示o所指向的地址里面存放的是一个指向（类型）的指针，适用于二维数组</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int a = 5;</span><br><span class="line">int *p = &amp;a;    //a的地址放在p中</span><br><span class="line">int **o = &amp;p;     //p的地址放在o中</span><br><span class="line">                  //所以a的地址放在o中</span><br></pre></td></tr></table></figure>

<h3 id="二-指针类型"><a href="#二-指针类型" class="headerlink" title="二.指针类型"></a>二.指针类型</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">printf(&quot;%d\n&quot;,sizeof(char*));</span><br><span class="line">printf(&quot;%d\n&quot;,sizeof(int*));</span><br><span class="line">printf(&quot;%d\n&quot;,sizeof(double*));</span><br><span class="line">printf(&quot;%d\n&quot;,sizeof(float*));</span><br></pre></td></tr></table></figure>

<p>这里的输出与系统有关，32位系统和64位系统的输出不同，但是输出是相同的，指针大小的输出与类型无关</p>
<h4 id="1-指针类型决定了指针进行解引用操作的的时候，访问空间大小"><a href="#1-指针类型决定了指针进行解引用操作的的时候，访问空间大小" class="headerlink" title="1.指针类型决定了指针进行解引用操作的的时候，访问空间大小"></a>1.<strong>指针类型决定了指针进行解引用操作的的时候，访问空间大小</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int *p ； #*p能够访问4个字节</span><br><span class="line">char *p ； #*p能够访问1个字节</span><br><span class="line">doube *p;  #*p能够访问8个字节</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int a = 0x11223344;  //八进制</span><br><span class="line">int *p1 = &amp;a;</span><br><span class="line">char *p2 = &amp;a;  //错误写法</span><br><span class="line">printf(&quot;%p\n&quot;,p1);</span><br><span class="line">printf(&quot;%p\n&quot;,p2);</span><br></pre></td></tr></table></figure>

<p>问题：<em>从“int *”到“char *”的类型不兼容</em></p>
<h4 id="2-指针类型决定了指针-1向后变化的字节"><a href="#2-指针类型决定了指针-1向后变化的字节" class="headerlink" title="2.指针类型决定了指针+1向后变化的字节"></a>2.<strong>指针类型决定了指针+1向后变化的字节</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int a = 0x11223344;</span><br><span class="line">int *p1 = &amp;a;</span><br><span class="line">char *p2 = &amp;a;</span><br><span class="line">printf(&quot;%p\n&quot;,p1+1);    </span><br><span class="line">printf(&quot;%p\n&quot;,p2+1);</span><br></pre></td></tr></table></figure>

<ul>
<li>int* 加1 ：地址+4</li>
<li>char *加1：地址+1</li>
<li>double*加1：地址+8</li>
</ul>
<h3 id="三-野指针（2种）"><a href="#三-野指针（2种）" class="headerlink" title="三.野指针（2种）"></a>三.野指针（2种）</h3><h4 id="1-指针未初始化"><a href="#1-指针未初始化" class="headerlink" title="1.指针未初始化"></a><strong>1.指针未初始化</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int main()</span><br><span class="line">&#123; </span><br><span class="line"> int *p;       //局部变量指针未初始化，默认为随机值</span><br><span class="line"> *p = 20;</span><br><span class="line"> return 0;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-指针越界"><a href="#2-指针越界" class="headerlink" title="2.指针越界"></a><strong>2.指针越界</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int num[10] = &#123;0&#125;;</span><br><span class="line">int *p = num;</span><br><span class="line">for(int i=0;i&lt;=11;i++)&#123;</span><br><span class="line">    *(p++) = i;   #超过数组范围</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><em>注意：在实际操作中应注意避开野指针</em></p>
<p>当指针不指向任何变量，可以这样写：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#define NULL 0   //文件开头指定符号常量NULL代表0</span><br><span class="line"></span><br><span class="line">int *p = NULL; //使p指向地址为0的单元，确保系统对它不起任何作用</span><br></pre></td></tr></table></figure>

<h3 id="四-通过指针引用数组"><a href="#四-通过指针引用数组" class="headerlink" title="四.通过指针引用数组"></a><strong>四.通过指针引用数组</strong></h3><ul>
<li><strong>数组</strong>：一个数组包含若干元素，每个数组元素都在内存中占用的存储单元，它们都有着相应的地址</li>
<li><strong>指针变量：</strong>既可以指向变量，也可以指向数组元素</li>
</ul>
<h4 id="1-指向数组元素的指针"><a href="#1-指向数组元素的指针" class="headerlink" title="1.指向数组元素的指针"></a>1.<strong>指向数组元素的指针</strong></h4><p>数组元素的指针：数组元素的地址</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int num[5] = &#123;1,2,3,4,5&#125;;</span><br><span class="line">int *p = num;</span><br><span class="line">//int *p = &amp;num[0];   数组的首元素地址赋值给指针变量p</span><br><span class="line">//int *p;*p = a;</span><br></pre></td></tr></table></figure>

<p><img src="" data-original="/../img/article/QQ%E6%88%AA%E5%9B%BE20220429101414.png"></p>
<h4 id="2-通过指针引用数组元素"><a href="#2-通过指针引用数组元素" class="headerlink" title="2.通过指针引用数组元素"></a><strong>2.通过指针引用数组元素</strong></h4><ul>
<li>下标法 如：a[i]</li>
<li>指针法 如：*(a+i)</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">int num[9]=&#123;1,2,3,4,5,6,7,8,9&#125;;</span><br><span class="line">int *p = num;</span><br><span class="line"></span><br><span class="line">printf(&quot;%p\n&quot;,p); </span><br><span class="line">printf(&quot;%p\n&quot;,p+1);</span><br><span class="line">printf(&quot;%p\n&quot;,p+2);</span><br><span class="line">printf(&quot;%d\n&quot;,*p);</span><br><span class="line">printf(&quot;%d\n&quot;,*(p+1));</span><br><span class="line">printf(&quot;%d\n&quot;,*(p+2));</span><br></pre></td></tr></table></figure>

<p>例子：输出以下结果</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int num[5] = &#123;1,2,3,4,5&#125;;</span><br><span class="line">int *p = num;</span><br><span class="line"></span><br><span class="line">printf(&quot;%d %d %d&quot;,*p,*p++,*++p);   //3 2 2</span><br></pre></td></tr></table></figure>

<p>对比</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int a = 1;</span><br><span class="line"></span><br><span class="line">printf(&quot;%d %d %d&quot;,a,a++,++a);   //3 2 3</span><br></pre></td></tr></table></figure>

<p><strong>总结：</strong>printf从右往左入栈，但是输出的顺序为：x或++x只改变x的值最后才输出，x++确定此位置的输出并立刻输出。指针：最后的*x和*++x并不是此时的*x的值，不会发生改变；其他：x最后计算完后，x和++x位置的值都是此时x的值。</p>
<p><strong>更多详情知识看这个：</strong><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_44194231/article/details/107007890">(32条消息) printf的压栈顺序和输出顺序_我在这里啊@的博客-CSDN博客_printf语句从右到左输出</a></p>
<h4 id="3-用数组名作函数参数"><a href="#3-用数组名作函数参数" class="headerlink" title="3.用数组名作函数参数"></a><strong>3.用数组名作函数参数</strong></h4><p>1.形参和实参都用数组名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">   int a[10];</span><br><span class="line">   func(a,10);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int func(int x[],int y)&#123;....&#125;</span><br></pre></td></tr></table></figure>

<p>2.实参用数组名，形参用指针变量</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">   int a[10];</span><br><span class="line">   func(a,10);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void func(int *p,int y)&#123;...&#125;</span><br></pre></td></tr></table></figure>

<p>3.实参形参都用指针变量</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">   int a[10],*p = a;</span><br><span class="line">   func(p,10);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void func(int *m,int y)&#123;...&#125;</span><br></pre></td></tr></table></figure>

<p>4.实参用指针变量，形参用数组名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">   int a[10],*p = a;</span><br><span class="line">   func(p,10);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void func(int m[],int y)&#123;...&#125;</span><br></pre></td></tr></table></figure>

<p>例子：下列运行后的输出结果是？</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">void func(char *c,int d)&#123;</span><br><span class="line">     *c = *c + 1;</span><br><span class="line">      d = d + 1;</span><br><span class="line">      printf(&quot;%c,%c\n&quot;,*c,d);</span><br><span class="line">&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">     char a = &#x27;A&#x27;,b = &#x27;a&#x27;;</span><br><span class="line">     func(&amp;b,a);</span><br><span class="line">     printf(&quot;%c,%c\n&quot;,a,b);</span><br><span class="line">     return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//输出：b,B</span><br><span class="line">        A,b</span><br></pre></td></tr></table></figure>

<h4 id="4-二维数组与指针（二维数组相当于二级指针）"><a href="#4-二维数组与指针（二维数组相当于二级指针）" class="headerlink" title="4.二维数组与指针（二维数组相当于二级指针）"></a>4.二维数组与指针（二维数组相当于二级指针）</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int a[3][4] = &#123;&#123;1,3,5,7&#125;,&#123;9,11,13,15&#125;,&#123;17,19,21,23&#125;&#125;;</span><br></pre></td></tr></table></figure>

<p><img src="" data-original="/../img/article/QQ%E5%9B%BE%E7%89%8720220417213513-1024x576.png"></p>
<p>在指向行指针前面加以一个*，就转换成为了列指针</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">*a</span><br><span class="line">*(a+1)</span><br></pre></td></tr></table></figure>

<p>在指向列的指针前面加一个&amp;，就转换成了行指针</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a[0]   //a[0]是以指向0行0列元素指针</span><br><span class="line">&amp;a[0]   //&amp;a[0]指向0行</span><br></pre></td></tr></table></figure>

<p>综合</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">printf(&quot;%p,%p\n&quot;,a,*a);     //0行起始地址   0行0列起始地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,a[0],*(a+0));     //0行0列元素地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,&amp;a[0],&amp;a[0][0]);      //0行起始地址  0行0列元素地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,a[1],a+1);         //1行0列元素地址  1行起始地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,&amp;a[1][0],*(a+1)+0);   //1行0列元素地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,a[2],*(a+2));   //2行0列元素地址</span><br><span class="line">printf(&quot;%p,%p\n&quot;,&amp;a[2],a+2);    // 2行起始地址</span><br><span class="line">printf(&quot;%d,%d\n&quot;,a[1][0],*(*(a+1)+0));   //1行0列元素</span><br><span class="line">printf(&quot;%d,%d\n&quot;,*a[2],*(*(a+2)+0));  //2行0列元素</span><br></pre></td></tr></table></figure>

<h3 id="五-数组指针和指针数组"><a href="#五-数组指针和指针数组" class="headerlink" title="五.数组指针和指针数组"></a><strong>五.数组指针和指针数组</strong></h3><p><strong>口诀：</strong>数组指针是指针，指针数组是数组</p>
<h4 id="1-数组指针"><a href="#1-数组指针" class="headerlink" title="1.数组指针"></a><strong>1.数组指针</strong></h4><p>数组指针：指向数组的指针，<strong>是指针</strong>，数组指针一般在二维数组中使用</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int (*p)[4];    //32位为4个字节，64位8个字节</span><br></pre></td></tr></table></figure>

<p><strong>解释：</strong>p先和*结合，说明p是一个指针变量，然后指着指向的是一个大小为10个整型的数组。所以p是一个 指针，指向一个数组的地址，叫数组指针。</p>
<p><strong>这里要注意：</strong>[]的优先级要高于*号的，所以必须加上（）来保证p先和*结合。</p>
<p><strong>一级指针和二级指针在数组指针的不同</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int num[2][2] = &#123;1,2,3,4&#125;;</span><br><span class="line">int *p = num;  //错误，一级指针不能与二级指针相等</span><br><span class="line">int (*p1)[2] = num;  //正确写法</span><br></pre></td></tr></table></figure>

<p>例子：输出二维数组的全部元素</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">    int num[3][4] = &#123;1,2,3,4,5,6,7,8,9,10,11,12&#125;;</span><br><span class="line">    int (*p)[4];   //定义一个指数组指针</span><br><span class="line">    p = num;   //num第0行的地址赋值给p</span><br><span class="line">    for(int i=0;i&lt;3;i++)&#123;</span><br><span class="line">    	for(int j=0;j&lt;4;j++)&#123;</span><br><span class="line">    	    printf(&quot;%d\t&quot;,*(*(p+i)+j));</span><br><span class="line">         &#125;</span><br><span class="line">         printf(&quot;\n&quot;);</span><br><span class="line">     &#125;</span><br><span class="line">    </span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-指针数组"><a href="#2-指针数组" class="headerlink" title="2.指针数组"></a><strong>2.指针数组</strong></h4><p>指针数组：一个存放指针的数组，<strong>是数组</strong>，数组指针里面放的每一个元素都放的是指针变量（地址），只能是地址；在使用的时候比较适合用来指向若干个字符串</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int *arr3[4]; //整形指针的数组，包含4个地址，32位16个字节，64位32字节</span><br></pre></td></tr></table></figure>

<p><img src="" data-original="/../img/article/QQ%E5%9B%BE%E7%89%8720220417134435.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int *arr3[4] =&#123;1,2,3,4&#125;;   //不能写成 </span><br><span class="line"></span><br><span class="line">int num = &#123;1,2,3,4,5&#125;;   //正确解</span><br><span class="line">int *arr[4] = &#123;&amp;num[0],&amp;num[1],&amp;num[2],&amp;num[3]&#125;;</span><br></pre></td></tr></table></figure>

<p>例子：对3个字符串按字母排序（从小到大）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;string.h&gt;</span><br><span class="line"></span><br><span class="line">void sort(char *p[],int a)&#123;    //形参为指针数组</span><br><span class="line">	char *temp;    //定义指针变量</span><br><span class="line">	for(int i=0;i&lt;3;i++)&#123;     //排序</span><br><span class="line">	   for(int j=0;j&lt;3-i;j++)&#123;</span><br><span class="line">	        if(strcmp(p[i],p[j])&gt;0)&#123;   //调用strcmp函数</span><br><span class="line">		    temp = p[i]; </span><br><span class="line">		    p[i] = p[j];</span><br><span class="line">		    p[j] = temp;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()&#123;</span><br><span class="line">    char *num[3]=&#123;&quot;Hello&quot;,&quot;World&quot;,&quot;Good&quot;&#125;;  //定义指针数组，分别指向3个字符串</span><br><span class="line">    sort(num,3);     //调用sort函数,传入num的地址和3</span><br><span class="line">    for(int i=0;i&lt;3;i++)&#123;</span><br><span class="line">	puts(num[i]);  //输出排好序的字符串</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-区别"><a href="#3-区别" class="headerlink" title="3.区别"></a>3.<strong>区别</strong></h4><p>数组指针只是一个指针变量，似乎是C语言里专门用来指向二维数组的，它占有内存中一个指针的存储空间。指针数组是多个指针变量，以数组形式存在内存当中，占有多个指针的存储空间</p>
<h3 id="六-字符指针（char-）"><a href="#六-字符指针（char-）" class="headerlink" title="六.字符指针（char*）"></a>六.字符指针（char*）</h3><h4 id="1-字符指针一般使用"><a href="#1-字符指针一般使用" class="headerlink" title="1.字符指针一般使用"></a>1.<strong>字符指针一般使用</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">(1)</span><br><span class="line">char ch = &#x27;w&#x27;;</span><br><span class="line">char *p = &amp;ch;</span><br><span class="line">*p = &#x27;q&#x27;;</span><br><span class="line"></span><br><span class="line">(2)</span><br><span class="line">char *p = &quot;Hello World&quot;;   //存储的是首元素地址</span><br><span class="line">printf(&quot;%s&quot;,p);</span><br></pre></td></tr></table></figure>

<p><em>注意：字符数组名或者字符指针变量可以输出一个字符串，但对于数值型(int)的是无法用数组名全部</em></p>
<h4 id="2-字数数组与字符指针变量的区别"><a href="#2-字数数组与字符指针变量的区别" class="headerlink" title="2.字数数组与字符指针变量的区别"></a>2.<strong>字数数组与字符指针变量的区别</strong></h4><p><strong>1.字符数组有若干个元素组成，每个元素中放一个字符，而字符指针变量存放的是指针地址（字符串第一个字符地址），绝不是将字符串放到指针变量中</strong></p>
<p><strong>2.赋值不同，字符数组不能使用以下方法赋值</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">char str[11];</span><br><span class="line">str = &quot;Hello World&quot;;  //数组名是地址不是常量，不能被赋值</span><br><span class="line">str[0] = &#x27;H&#x27;;    //但是可以对单个数组元素进行赋值</span><br></pre></td></tr></table></figure>

<p>而字符指针变量可以</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">char *str;</span><br><span class="line">str = &quot;Hello World&quot;;   </span><br><span class="line">//赋值给str是字符串第一个元素地址，不是字符</span><br></pre></td></tr></table></figure>

<p>3**.初始化不同**</p>
<p>对于字符指针：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char *a = &quot;Hello World&quot;;</span><br></pre></td></tr></table></figure>

<p>等价于</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">char *a;</span><br><span class="line">a = &quot;Hello World&quot;;  //把H的地址赋给a</span><br><span class="line">注意不能写成： *a = &quot;Hello World&quot;;</span><br></pre></td></tr></table></figure>

<p>对于字符数组：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char a[12] = &quot;Hello World&quot;;</span><br></pre></td></tr></table></figure>

<p>不等价于</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">char a[12];</span><br><span class="line">a = &quot;Hello World&quot;;   //不能把字符串一次性全部赋值</span><br></pre></td></tr></table></figure>

<p><strong>4.储存不同</strong></p>
<ul>
<li>字符数组分配了若干个空间，用来存放个元素值</li>
<li>字符指针变量只分配了一个储存空间</li>
</ul>
<p>5**.指针变量的值是可以改变的，但是字符数组是一个固定的值（首元素地址），无法改变**</p>
<p><strong>例子：将字符串A赋值给字符串B</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">int main()&#123;</span><br><span class="line">    char num1[12] =&quot;Hello World&quot;,num2[12],*p1,*p2;</span><br><span class="line">    p1 = num1;</span><br><span class="line">    p2 = num2;</span><br><span class="line">    while(*p1!=&#x27;\0&#x27;)&#123;</span><br><span class="line">         *p2 = *p1;</span><br><span class="line">         *p1++;*p2++;     //*p++  ==&gt;  *(p++)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    printf(&quot;num1: %s\n&quot;,num1);</span><br><span class="line">    printf(&quot;num2: %s\n&quot;,num2);</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="七-指向函数的指针（一个指针）"><a href="#七-指向函数的指针（一个指针）" class="headerlink" title="七.指向函数的指针（一个指针）"></a>七.指向函数的指针（一个指针）</h3><p><em>函数名代表函数的起始地址</em></p>
<h4 id="1-定义指向函数的指针变量"><a href="#1-定义指向函数的指针变量" class="headerlink" title="1.定义指向函数的指针变量"></a>1.<strong>定义指向函数的指针变量</strong></h4><p>类型 （*指针变量名）（函数参数列表） <em>*指针变量名要括起来</em></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int (*p)(int a,int b);    //表示指针变量p指向返回值为整数且有两个整型参数的函数</span><br></pre></td></tr></table></figure>

<p>是一个指针，指向一个函数</p>
<h4 id="2-用函数指针变量调用函数"><a href="#2-用函数指针变量调用函数" class="headerlink" title="2.用函数指针变量调用函数"></a>2.用函数指针变量调用函数</h4><p>例子：求两数中最大值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">int max(int a ,int b)&#123;</span><br><span class="line">	return a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">int main()&#123;</span><br><span class="line">	int a,b;</span><br><span class="line">	int (*p)(int ,int);      //定义指向函数的指针变量</span><br><span class="line">	scanf(&quot;%d %d&quot;,&amp;a,&amp;b);</span><br><span class="line">	p = max;      //使p指向max函数，max函数的入口地址赋给了指针变量p</span><br><span class="line">	printf(&quot;最大值：%d&quot;,(*p)(a,b));</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-用指向函数的指针作为函数的参数"><a href="#2-用指向函数的指针作为函数的参数" class="headerlink" title="2.用指向函数的指针作为函数的参数"></a>2.用指向函数的指针作为函数的参数</h4><p><strong>指向函数的指针变量的一个重要途径：把函数的入口地址作为参数传到其他函数</strong></p>
<p>例如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">void func(int (*p1)(int,int),int (*p2)(int,int))&#123;</span><br><span class="line">	int a,b;</span><br><span class="line">	a = (*p1)(1,3);   //调用p1函数</span><br><span class="line">	b = (*p2)(2,5);    //调用p2函数</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>例子：求两数中最大值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">int max(int a ,int b)&#123;</span><br><span class="line">	return a&gt;b?a:b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int func(int a,int b,int (*p)(int,int))&#123;    //相当于 p = max;</span><br><span class="line">     return (*p)(a,b);   //调用max函数，传入a，b并返回，相当于max(a,b);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()&#123;</span><br><span class="line">    int max(int,int);   //声明max函数</span><br><span class="line">    int func(int,int,int (*p)(int,int));//声明func函数，传入参为指向函数的指针变量</span><br><span class="line">    int a,b;</span><br><span class="line">    scanf(&quot;%d %d&quot;,&amp;a,&amp;b);</span><br><span class="line">    printf(&quot;%d&quot;,func(a,b,max));  //传参 a,b,max函数的地址</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="八-返回指针值的函数（一个函数）"><a href="#八-返回指针值的函数（一个函数）" class="headerlink" title="八.返回指针值的函数（一个函数）"></a>八.返回指针值的函数（一个函数）</h3><p><strong>注意：</strong>一个函数可以返回整型值，字符值，实类值等。这个函数与以前的函数类似，只不过返回的是指针型（地址）</p>
<h4 id="一般形式：类型名-函数名-参数列表"><a href="#一般形式：类型名-函数名-参数列表" class="headerlink" title="一般形式：类型名 *函数名(参数列表)"></a>一般形式：<strong>类型名 *函数名(参数列表)</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int *func(int x,int y)</span><br></pre></td></tr></table></figure>

<p>解释：a是函数名，调用他以后得到一个int*型（指向整型数据）的指针。x，y是整型形参。</p>
<p><strong>例子：给定一个多维数组，输入行数，输出他的一行的所有元素</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">int *find(int (*a)[4],int n)&#123;   //形参a是指向一维数组的数组指针，存放的是首元素地址</span><br><span class="line">	int *b;</span><br><span class="line">	b = *(a+n);    //b的值是&amp;num[k][0]</span><br><span class="line">	return (b);  //返回一个指针</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()&#123;</span><br><span class="line">	int num[3][4] = &#123;97,98,96,95,94,93,92,91,90,89,88,87&#125;;</span><br><span class="line">	int *p,k;</span><br><span class="line">	printf(&quot;输入行数：&quot;); </span><br><span class="line">	scanf(&quot;%d&quot;,&amp;k);</span><br><span class="line">	p = find(num,k);    //返回num[k][0]的地址</span><br><span class="line">	for(int i=0;i&lt;4;i++)&#123;</span><br><span class="line">	    printf(&quot;%d &quot;,*(p+i));   //依次输出第k行的所有元素</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://www.lijunxi.site">Jixer</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://www.lijunxi.site/posts/3447396109/">http://www.lijunxi.site/posts/3447396109/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://www.lijunxi.site" target="_blank">Jixer的小屋</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a></div><div class="post_share"><div class="social-share" data-image="https://q1.qlogo.cn/g?b=qq&amp;nk=2770063826&amp;s=640" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="/pluginsSrc/butterfly-extsrc/sharejs/dist/css/share.min.css?v=1.1.3" media="print" onload="this.media='all'"><script src="/pluginsSrc/butterfly-extsrc/sharejs/dist/js/social-share.min.js?v=1.1.3" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/695896537/" title="Python学习笔记"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python学习笔记</div></div></a></div><div class="next-post pull-right"><a href="/posts/388766253/" title="ElasticSearch学习笔记"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">ElasticSearch学习笔记</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/3014530432/" title="C语言错题集"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-01-17</div><div class="title">C语言错题集</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="" data-original="https://q1.qlogo.cn/g?b=qq&amp;nk=2770063826&amp;s=640" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Jixer</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/2770063826"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content"></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80-%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F"><span class="toc-text">一.指针变量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E4%B8%80%E7%BA%A7%E6%8C%87%E9%92%88"><span class="toc-text">1.一级指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E4%BA%8C%E7%BA%A7%E6%8C%87%E9%92%88%EF%BC%88%E6%8C%87%E5%90%91%E6%8C%87%E9%92%88%E7%9A%84%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%EF%BC%89"><span class="toc-text">2.二级指针（指向指针的指针变量）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C-%E6%8C%87%E9%92%88%E7%B1%BB%E5%9E%8B"><span class="toc-text">二.指针类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%8C%87%E9%92%88%E7%B1%BB%E5%9E%8B%E5%86%B3%E5%AE%9A%E4%BA%86%E6%8C%87%E9%92%88%E8%BF%9B%E8%A1%8C%E8%A7%A3%E5%BC%95%E7%94%A8%E6%93%8D%E4%BD%9C%E7%9A%84%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E8%AE%BF%E9%97%AE%E7%A9%BA%E9%97%B4%E5%A4%A7%E5%B0%8F"><span class="toc-text">1.指针类型决定了指针进行解引用操作的的时候，访问空间大小</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%8C%87%E9%92%88%E7%B1%BB%E5%9E%8B%E5%86%B3%E5%AE%9A%E4%BA%86%E6%8C%87%E9%92%88-1%E5%90%91%E5%90%8E%E5%8F%98%E5%8C%96%E7%9A%84%E5%AD%97%E8%8A%82"><span class="toc-text">2.指针类型决定了指针+1向后变化的字节</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89-%E9%87%8E%E6%8C%87%E9%92%88%EF%BC%882%E7%A7%8D%EF%BC%89"><span class="toc-text">三.野指针（2种）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%8C%87%E9%92%88%E6%9C%AA%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">1.指针未初始化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%8C%87%E9%92%88%E8%B6%8A%E7%95%8C"><span class="toc-text">2.指针越界</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B-%E9%80%9A%E8%BF%87%E6%8C%87%E9%92%88%E5%BC%95%E7%94%A8%E6%95%B0%E7%BB%84"><span class="toc-text">四.通过指针引用数组</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%8C%87%E5%90%91%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%9A%84%E6%8C%87%E9%92%88"><span class="toc-text">1.指向数组元素的指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E9%80%9A%E8%BF%87%E6%8C%87%E9%92%88%E5%BC%95%E7%94%A8%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0"><span class="toc-text">2.通过指针引用数组元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E7%94%A8%E6%95%B0%E7%BB%84%E5%90%8D%E4%BD%9C%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0"><span class="toc-text">3.用数组名作函数参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E4%B8%8E%E6%8C%87%E9%92%88%EF%BC%88%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9B%B8%E5%BD%93%E4%BA%8E%E4%BA%8C%E7%BA%A7%E6%8C%87%E9%92%88%EF%BC%89"><span class="toc-text">4.二维数组与指针（二维数组相当于二级指针）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94-%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E5%92%8C%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="toc-text">五.数组指针和指针数组</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88"><span class="toc-text">1.数组指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="toc-text">2.指针数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%8C%BA%E5%88%AB"><span class="toc-text">3.区别</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AD-%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88%EF%BC%88char-%EF%BC%89"><span class="toc-text">六.字符指针（char*）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88%E4%B8%80%E8%88%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">1.字符指针一般使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%AD%97%E6%95%B0%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">2.字数数组与字符指针变量的区别</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%83-%E6%8C%87%E5%90%91%E5%87%BD%E6%95%B0%E7%9A%84%E6%8C%87%E9%92%88%EF%BC%88%E4%B8%80%E4%B8%AA%E6%8C%87%E9%92%88%EF%BC%89"><span class="toc-text">七.指向函数的指针（一个指针）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%AE%9A%E4%B9%89%E6%8C%87%E5%90%91%E5%87%BD%E6%95%B0%E7%9A%84%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F"><span class="toc-text">1.定义指向函数的指针变量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%94%A8%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-text">2.用函数指针变量调用函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%94%A8%E6%8C%87%E5%90%91%E5%87%BD%E6%95%B0%E7%9A%84%E6%8C%87%E9%92%88%E4%BD%9C%E4%B8%BA%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-text">2.用指向函数的指针作为函数的参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AB-%E8%BF%94%E5%9B%9E%E6%8C%87%E9%92%88%E5%80%BC%E7%9A%84%E5%87%BD%E6%95%B0%EF%BC%88%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">八.返回指针值的函数（一个函数）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E8%88%AC%E5%BD%A2%E5%BC%8F%EF%BC%9A%E7%B1%BB%E5%9E%8B%E5%90%8D-%E5%87%BD%E6%95%B0%E5%90%8D-%E5%8F%82%E6%95%B0%E5%88%97%E8%A1%A8"><span class="toc-text">一般形式：类型名 *函数名(参数列表)</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/2029624507/" title="2022年算法队选拔赛">2022年算法队选拔赛</a><time datetime="2024-05-09T15:00:27.000Z" title="发表于 2024-05-09 23:00:27">2024-05-09</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/1978524057/" title="牛客小白月赛84">牛客小白月赛84</a><time datetime="2024-05-08T14:40:35.000Z" title="发表于 2024-05-08 22:40:35">2024-05-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/131339317/" title="软件测试资料">软件测试资料</a><time datetime="2024-05-07T03:12:52.000Z" title="发表于 2024-05-07 11:12:52">2024-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/2394234105/" title="第十四届蓝桥杯B组国赛">第十四届蓝桥杯B组国赛</a><time datetime="2024-05-05T13:40:15.000Z" title="发表于 2024-05-05 21:40:15">2024-05-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/1405472621/" title="Leetcode第396场周赛">Leetcode第396场周赛</a><time datetime="2024-05-05T03:58:25.000Z" title="发表于 2024-05-05 11:58:25">2024-05-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2024 By Jixer</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="https://beian.miit.gov.cn/#/Integrated/index" style="color:white" target="_blank">蜀ICP备2022009955号-1</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/pluginsSrc/@fancyapps/ui/dist/fancybox/fancybox.umd.js?v=5.0.33"></script><div class="js-pjax"></div><script src="/js/custom-fancybox-umd-min.js"></script><script src="/js/custom-busuanzi-pure-mini.js"></script><script src="/js/Valine.min.js"></script><script src="/js/custom-social-share.min.js"></script><script src="/js/custom-typed-umd-min.js"></script><script src="/js/av-min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body></html>